<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2021/1/18
 * Time: 16:01
 */

namespace app\admin\controller;


use think\Db;
use think\Request;

class Store extends Base
{
    /**
     * 店铺列表展示
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(request()->isAjax ()){
            $store_name = trim(input('store_name'), ' '); // 店铺名
            $store_user = trim(input('store_user'), ' '); // 店主
            $store_real_user = trim(input('store_real_user'), ' '); // 实际经营者
            
            $where['store_status'] = ['NEQ', 3];
            if(isset($store_name) && $store_name != ''){
                $where['store_name'] = ['like', '%' . $store_name . '%'];
            }
            if(isset($store_user) && $store_user != ''){
                $where['u1.user_real_name'] = ['like', '%' . $store_user . '%'];
            }
            if(isset($store_real_user) && $store_real_user != ''){
                $where['u2.user_real_name'] = ['like', '%' . $store_real_user . '%'];
            }
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('store')
                ->join('up_u_user u1', 'u1.user_id = store_user_id', 'LEFT')
                ->join('up_u_user u2', 'u2.user_id = store_real_user_id', 'LEFT')
                ->join('up_area ua1', 'ua1.district_id = store_sheng', 'LEFT')
                ->join('up_area ua2', 'ua2.district_id = store_shi', 'LEFT')
                ->join('up_area ua3', 'ua3.district_id = store_qu', 'LEFT')
                ->where($where)
                ->count('store_id');// 获取总条数
            $lists = Db::name('store')
                ->join('up_u_user u1', 'u1.user_id = store_user_id', 'LEFT')
                ->join('up_u_user u2', 'u2.user_id = store_real_user_id', 'LEFT')
                ->join('up_area ua1', 'ua1.district_id = store_sheng', 'LEFT')
                ->join('up_area ua2', 'ua2.district_id = store_shi', 'LEFT')
                ->join('up_area ua3', 'ua3.district_id = store_qu', 'LEFT')
                ->field('store_id, store_name, u1.user_real_name store_user, u2.user_real_name store_real_name, 
                store_phone, store_status, store_remark, ua1.district sheng, ua2.district shi, ua3.district qu, 
                store_address')
                ->where($where)
                ->page($Nowpage,$limits)
                ->order('store_time desc')
                ->select();

            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }
    
    /**
     * 添加店铺
     * @return mixed|\think\response\Json
     */
    public function storeAdd(){
        if(Request::instance()->isPost()){
            $store_name = trim(input('store_name'), ' ');
            $store_phone = trim(input('store_phone'), ' ');
            $store_user_id = input('store_user_id');
            $store_real_user_id = input('store_real_user_id');
            $store_sheng = input('store_sheng');
            $store_shi = input('store_shi');
            $store_qu = input('store_qu');
            $store_address = input('store_address');
            $store_lon = input('store_lon');
            $store_lat = input('store_lat');
            $store_remark = input('store_remark');
            
            $insert = [
                'store_name' => $store_name,
                'store_user_id' => $store_user_id,
                'store_real_user_id' => $store_real_user_id,
                'store_phone' => $store_phone,
                'store_sheng' => $store_sheng,
                'store_shi' => $store_shi,
                'store_qu' => $store_qu,
                'store_address' => $store_address,
                'store_remark' => $store_remark,
                'store_lon' => $store_lon,
                'store_lat' => $store_lat,
                'store_time' => date('Y-m-d H:i:s')
            ];
            $res = Db::table('up_store')->insert($insert);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        $user = Db::table('up_u_user')
            ->where('user_status', 1)
            ->field('user_username, user_real_name, user_phone, user_id')
            ->select();
        $province = Db::table('up_area')->where('level', 1)->field('district_id, district')->select();
        return $this->fetch('store/storeadd', ['user' => $user, 'province' => $province]);
    }
    
    /**
     * 编辑店铺
     * @return mixed|\think\response\Json
     */
    public function storeEdit(){
        $store_id = input('store_id');
        if(Request::instance()->isPost()){
            $store_name = trim(input('store_name'), ' ');
            $store_phone = trim(input('store_phone'), ' ');
            $store_user_id = input('store_user_id');
            $store_real_user_id = input('store_real_user_id');
            $store_sheng = input('store_sheng');
            $store_shi = input('store_shi');
            $store_qu = input('store_qu');
            $store_address = input('store_address');
            $store_lon = input('store_lon');
            $store_lat = input('store_lat');
            $store_remark = input('store_remark');
            $update = [
                'store_name' => $store_name,
                'store_user_id' => $store_user_id,
                'store_real_user_id' => $store_real_user_id,
                'store_phone' => $store_phone,
                'store_sheng' => $store_sheng,
                'store_shi' => $store_shi,
                'store_qu' => $store_qu,
                'store_address' => $store_address,
                'store_remark' => $store_remark,
                'store_lon' => $store_lon,
                'store_lat' => $store_lat,
                'store_updatetime' => date('Y-m-d H:i:s')
            ];
            $res = Db::table('up_store')->where('store_id', $store_id)->update($update);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        $user = Db::table('up_u_user')
            ->where('user_status', 1)
            ->field('user_username, user_real_name, user_phone, user_id')
            ->select();
        $store = Db::table('up_store')
            ->where('store_id', $store_id)
            ->field('store_name, store_user_id, store_real_user_id, store_phone, store_sheng, store_shi, 
            store_qu, store_address, store_remark, store_lon, store_lat')
            ->find();
        $province = Db::table('up_area')->where('level', 1)->field('district_id, district')->select();
        $city = Db::table('up_area')->where('pid', $store['store_sheng'])->field('district_id, district')->select();
        $district = Db::table('up_area')->where('pid', $store['store_shi'])->field('district_id, district')->select();

        $this->assign([
            'user' => $user,
            'province' => $province,
            'city' => $city,
            'district' => $district,
            'store' => $store,
            'store_id' => $store_id
        ]);
        return $this->fetch('store/storeedit');
    }
    
    /**
     * 停止运营
     * @return \think\response\Json
     */
    public function storeDown(){
        $store_id = input('id');
        $res = Db::table('up_store')->where('store_id', $store_id)->setField('store_status', 2);
        if($res){
            return json(['code' => 200, 'msg' => '操作成功']);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }
    
    /**
     * 正常运营
     * @return \think\response\Json
     */
    public function storeUp(){
        $store_id = input('id');
        $res = Db::table('up_store')->where('store_id', $store_id)->setField('store_status', 1);
        if($res){
            return json(['code' => 200, 'msg' => '操作成功']);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }
    
    /**
     * 删除店铺
     * @return \think\response\Json
     */
    public function storeDel(){
        $store_id = input('id');
        $res = Db::table('up_store')->where('store_id', $store_id)->setField('store_status', 3);
        if($res){
            return json(['code' => 200, 'msg' => '操作成功']);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }
    
    /**
     * 导入Excel
     * @return mixed
     */
    public function excelIn(){
        set_time_limit(0);
        if(request()->file('file')){
            $file = request()->file('file');
            $info  = $file->getInfo();
            $file_path = $info['tmp_name'];
            //取出文件后缀
            $file_type = explode(".",$info['name']);
            $file_type = end($file_type);
            vendor("PHPExcel.PHPExcel.IOFactory");
            if( $file_type == 'xls' ){
                $inputFileType = 'Excel5';    //这个是读 xls的
            }else{
                $inputFileType = 'Excel2007';//这个是计xlsx的
            }
            $objReader = \PHPExcel_IOFactory::createReader($inputFileType);
            $objPHPExcel = $objReader->load($file_path);
            $objWorksheet = $objPHPExcel->getActiveSheet();
            $highestRow = $objWorksheet->getHighestRow();
//            print_r($highestRow);die;
            $time = date('Y-m-d H:i:s');
            Db::startTrans();
            try{
                for ($i = 2; $i <= $highestRow; $i++) {
                    $a = $objPHPExcel->getActiveSheet ()->getCell ("A" . $i)->getValue ();//获取A列的值
                    $b = $objPHPExcel->getActiveSheet ()->getCell ("B" . $i)->getValue ();//获取B列的值
                    $c = $objPHPExcel->getActiveSheet ()->getCell ("C" . $i)->getValue ();//获取C列的值
                    $d = $objPHPExcel->getActiveSheet ()->getCell ("D" . $i)->getValue ();//获取D列的值
                    $e = $objPHPExcel->getActiveSheet ()->getCell ("E" . $i)->getValue ();//获取E列的值
                    $f = $objPHPExcel->getActiveSheet ()->getCell ("F" . $i)->getValue ();//获取F列的值
                    $g = $objPHPExcel->getActiveSheet ()->getCell ("G" . $i)->getValue ();//获取G列的值
                    $h = $objPHPExcel->getActiveSheet ()->getCell ("H" . $i)->getValue ();//获取H列的值
                    $k = $objPHPExcel->getActiveSheet ()->getCell ("I" . $i)->getValue ();//获取I列的值
                    $l = $objPHPExcel->getActiveSheet ()->getCell ("J" . $i)->getValue ();//获取G列的值
                    $m = $objPHPExcel->getActiveSheet ()->getCell ("K" . $i)->getValue ();//获取K列的值
                    $n = $objPHPExcel->getActiveSheet ()->getCell ("L" . $i)->getValue ();//获取K列的值
                    $o = $objPHPExcel->getActiveSheet ()->getCell ("M" . $i)->getValue ();//获取K列的值
                    $a = trim($a, ' '); // 店铺名称
                    $b = trim($b, ' '); // 负责人ID
                    $c = trim($c, ' '); // 实际经营者ID
                    $d = trim($d, ' '); // 联系方式
                    $e = trim($e, ' '); // 省ID
                    $f = trim($f, ' '); // 市ID
                    $g = trim($g, ' '); // 区ID
                    $h = trim($h, ' '); // 街道ID
                    $k = trim($k, ' '); // 详细地址
                    $l = trim($l, ' '); // 运营状态
                    $m = trim($m, ' '); // 经度
                    $n = trim($n, ' '); // 纬度
                    
                    if($a != '' && $b != '' && $c != '' && $d != '' && $e != '' && $f != '' &&
                        $g != '' && $k != '' && $l != '' && $m != '' && $n != ''){
                        $store = [
                            'store_name' => $a,
                            'store_user_id' => $b,
                            'store_real_user_id' => $c,
                            'store_phone' => $d,
                            'store_sheng' => $e,
                            'store_shi' => $f,
                            'store_qu' => $g,
                            'store_address' => $k,
                            'store_status' => $l,
                            'store_remark' => $o,
                            'store_lon' => $m,
                            'store_lat' => $n,
                            'store_time' => $time
                        ];
                        Db::table('up_store')->insert($store);
                    }
                }
                Db::commit();
                echo '/admin/Store/index';
            }catch(\Exception $e){
                Db::rollback();
            }
        }
        return $this->fetch('store/excelin');
    }
    
    /**
     * 导出excel
     * @return \think\response\Json
     */
    public function outExcel(){
        $store_ids = input('ids');

        $where['store_status'] = ['NEQ', 3];
        if($store_ids == -1){
            $store_name = trim(input('store_name'), ' '); // 店铺名
            $store_user = trim(input('store_user'), ' '); // 店主
            $store_real_user = trim(input('store_real_user'), ' '); // 实际经营者

            if(isset($store_name) && $store_name != ''){
                $where['store_name'] = ['like', '%' . $store_name . '%'];
            }
            if(isset($store_user) && $store_user != ''){
                $where['u1.user_real_name'] = ['like', '%' . $store_user . '%'];
            }
            if(isset($store_real_user) && $store_real_user != ''){
                $where['u2.user_real_name'] = ['like', '%' . $store_real_user . '%'];
            }
        }else{
            if($store_ids == ''){
                return json(['code' => 100, 'msg' => '请选择需要导出的数据']);
            }
            $where['store_id'] = ['IN', $store_ids];
        }

        $list = Db::name('store')
            ->join('up_u_user u1', 'u1.user_id = store_user_id', 'LEFT')
            ->join('up_u_user u2', 'u2.user_id = store_real_user_id', 'LEFT')
            ->field('store_id, store_name, u1.user_real_name store_user, u2.user_real_name store_real_name, 
                store_phone, store_status, store_remark, store_address, store_lon, store_lat')
            ->where($where)
            ->select();
        
        foreach($list as $k => $v){
            // 店铺状态
            $list[$k]['store_status'] = $v['store_status'] == 1? '正在运营' : '停止运营';
            // 手机号
            $lists[$k]['store_phone'] = "\t".$v['store_phone']."\t";
        }
        $cellname = [
            ['store_user', '负责人姓名', 15, 'LEFT'],
            ['store_real_name', '实际经营者姓名', 20, 'LEFT'],
            ['store_phone', '联系方式', 15, 'LEFT'],
            ['store_name', '店铺名称', 20, 'LEFT'],
            ['store_status', '店铺状态', 15, 'LEFT'],
            ['store_address', '地址', 30, 'LEFT'],
            ['store_lon', '经度', 15, 'LEFT'],
            ['store_lat', '纬度', 15, 'LEFT'],
            ['store_remark', '其它信息', 30, 'LEFT'],
        ];
        $res = exportExcelZhy('门店表', 'Store', $cellname, $list);
        return json($res);
    }
    
    
    
    
    
    
    
    
    
    
    
}